Thread: [beginner] Proper capitalization

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    Post [beginner] Proper capitalization

    Hello there, trying to write a program to use proper capitalization on an input sentance. Here's what I have but its compiling and giving me back gobbildy-gook, can someone lend a hand? cheers. Shane.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
            char text[200];
            int i = 0;
            
            printf("Enter text:");
            gets(text);
    
    
    /*First letter is always capital*/
    
    
        if(text[i] > 96 && text[i] < 123)
        {
                text[i] = text[i] - 32;
                printf("%c", text[i]);
                i++;
        }
        else
        {
                printf("%c", text[i]);
        }
    
    
    /*Eliminating random capitals throughout the sentance*/
        while(text[i] != '\0')
        {
            if(text[i] > 64 && text[i] < 91);
            {
                    text[i] = text[i] + 32;
                    printf("%c", text[i]);
            }
            
            if(text[i] > 96 && text[i] < 123)
            {
                    printf("%c", text[i]);
            }        
            
            if(text[i] == '.')        //Capitalizing after a full stop
            {
                    i = i + 2;        //i + 2 because there is a space after a full stop
                
                if(text[i] > 64 && text[i] < 91);
                {
                    text[i] = text[i] + 32;
                    printf("%c", text[i]);
                }
                
                if(text[i] > 96 && text[i] < 123)
                {
                    printf("%c", text[i]);
                }        
            }
                i++;
        }
        
            
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Line 10: Don't use "gets()" (FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com)
    Line 16: It would be better to use existing functions such as "isalpha()" and "toupper()", for clarity and especially portability
    Line 31: You have a semi-colon after the "if()", which means the following code will always be executed
    Line 31: See notes for "Line 16"
    Line 37: See notes for "Line 16"
    Line 44: You should check for where the next letter is after the period, rather than relying on the assumption that there is just a single space after a period (for instance, I use two spaces after a period - in an input, someone can use any number of spaces after a period, including none)
    Line 46: You have a semi-colon after the "if()", which means the following code will always be executed
    Line 46: See notes for "Line 16"
    Line 52: See notes for "Line 16"

    Fix these things up and see if you get closer to a working implementation.
    Last edited by Matticus; 04-14-2014 at 02:40 PM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You should consider looking up toupper and tolower in ctype.h. They will be very useful to you in this project.

    On a related note, what is considered proper capitalization? Is it just the first letter, or are you expected to capitalize proper names as well?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    This is a pretty interesting idea.

    if (char string[scanning int]>='A' && char string [scanning int] <='Z') It is ok to use 'a' or 'A' in an expression like this (with ascii anyway), so long as you have the ' ' around it to indicate constant character.

    just looking at the output I got for it, it looks like you have at least 2 if loops operating on the same data. You could try setting it to print out the decimal values, feeding it different inputs, and tracing back through the program to find out what each if loop is doing to each variable.

  5. #5
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I just started rewriting this into my own thing and noticed some things about your version that might help you in the future.

    1. This sequence is before the while loop that limits the variable I (<-auto correct) from walking off the end of the array. Also, the intention isn't clear, you covered all cases where a letter and several symbols might be entered, and then subtracted 32 from everything. So lower case will be capitalized and everything else starts right after null.
    Code:
        if(text[i] > 96 && text[i] < 123)
        {
                text[i] = text[i] - 32;
                printf("%c", text[i]);
                i++;
        }
        else
        {
                printf("%c", text[i]);
        }
    


    2. The 1st& 4th, and the 2&5 if statements are the same.

    3. I think the purpose was to capitalize things 2 spaces after a period, but you didn't make any changes to the array itself. Only caused the I variable to skip those spaces.
    Code:
            if(text[i] == '.')       
            {
                    i = i + 2;
    
    


    4. When printing a character array it is ok to use the %s format, and name only the array (without the [I] scanning element added)

    Hope any of this was helpful, and good luck!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] My capitalization program is not working
    By Arbyn Acosta in forum C Programming
    Replies: 8
    Last Post: 08-12-2012, 08:26 AM
  2. Capitalization
    By jacek in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:03 PM
  3. Standard for header file naming/capitalization?
    By skorman00 in forum C Programming
    Replies: 4
    Last Post: 04-21-2006, 02:38 AM
  4. capitalization trick
    By volk in forum C++ Programming
    Replies: 11
    Last Post: 04-05-2003, 07:12 PM
  5. Capitalization help
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-20-2001, 12:39 AM